package com.joolun.cloud.upms.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joolun.cloud.common.core.constant.CommonConstants;
import com.joolun.cloud.common.core.util.R;
import com.joolun.cloud.common.data.tenant.TenantContextHolder;
import com.joolun.cloud.common.log.annotation.SysLog;
import com.joolun.cloud.common.security.annotation.Inside;
import com.joolun.cloud.upms.admin.service.ShopUserService;
import com.joolun.cloud.upms.admin.service.SysRoleService;
import com.joolun.cloud.upms.admin.service.SysUserRoleService;
import com.joolun.cloud.upms.admin.service.SysUserService;
import com.joolun.cloud.upms.common.entity.SysRole;
import com.joolun.cloud.upms.common.entity.SysUser;
import com.joolun.cloud.upms.common.entity.SysUserRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shopuser")
@Api(value = "shopuser", tags = "店铺用户管理模块")
public class ShopUserController {
	private final ShopUserService shopUserService;
	private final SysUserService sysUserService;
	private final SysRoleService sysRoleService;
	private final SysUserRoleService sysUserRoleService;

	/**
	 * 分页列表
	 * @param page 分页对象
	 * @param sysUser
	 * @return
	 */
	@ApiOperation(value = "分页列表")
	@GetMapping("/page")
	@PreAuthorize("@ato.hasAuthority('mall:shopuser:index')")
	public R getPage(Page page, SysUser sysUser) {
		return R.ok(shopUserService.page(page, Wrappers.query(sysUser)));
	}

	/**
	 * 店铺用户查询
	 * @param id
	 * @return R
	 */
	@ApiOperation(value = "店铺用户查询")
	@GetMapping("/{id}")
	@PreAuthorize("@ato.hasAuthority('mall:shopuser:get')")
	public R getById(@PathVariable("id") String id) {
		return R.ok(shopUserService.getById(id));
	}

	/**
	 * 店铺用户新增
	 * @param sysUser
	 * @return R
	 */
	@ApiOperation(value = "店铺用户新增")
	@SysLog("新增店铺用户")
	@PostMapping
	@PreAuthorize("@ato.hasAuthority('mall:shopuser:add')")
	public R save(@RequestBody SysUser sysUser) {
		sysUser.setType(CommonConstants.USER_TYPE_2);
		sysUser.setOrganId(TenantContextHolder.getTenantId());
		//获取店员角色信息
		SysRole sysRole = sysRoleService.getOne(Wrappers.<SysRole>update().lambda()
				.eq(SysRole::getRoleCode,CommonConstants.ROLE_CODE_SHOP_USER));
		if(sysRole == null){
			return R.failed("请先添加角色编码为【"+CommonConstants.ROLE_CODE_SHOP_USER+"】的店员角色");
		}
		return R.ok(shopUserService.save(sysUser,sysRole.getId()));
	}

	/**
	 * 店铺用户修改
	 * @param sysUser
	 * @return R
	 */
	@ApiOperation(value = "店铺用户修改")
	@SysLog("修改店铺用户")
	@PutMapping
	@PreAuthorize("@ato.hasAuthority('mall:shopuser:edit')")
	public R updateById(@RequestBody SysUser sysUser) {
		sysUser.setType(CommonConstants.USER_TYPE_2);
		return R.ok(shopUserService.updateById(sysUser));
	}

	/**
	 * 店铺用户删除
	 * @param id
	 * @return R
	 */
	@ApiOperation(value = "店铺用户删除")
	@SysLog("删除店铺用户")
	@DeleteMapping("/{id}")
	@PreAuthorize("@ato.hasAuthority('mall:shopuser:del')")
	public R removeById(@PathVariable String id) {
		//拥有管理员角色的用户不让删除
		SysRole sysRole = sysRoleService.getOne(Wrappers.<SysRole>update().lambda()
				.eq(SysRole::getRoleCode,CommonConstants.ROLE_CODE_ADMIN));
		List<SysUserRole> listSysUserRole = sysUserRoleService.list(Wrappers.<SysUserRole>update().lambda()
				.eq(SysUserRole::getUserId,id).eq(SysUserRole::getRoleId,sysRole.getId()));
		if(listSysUserRole.size()>0){
			return R.failed("无法删除拥有管理员角色的用户");
		}
		SysUser sysUser = sysUserService.getById(id);
		return R.ok(sysUserService.deleteUserById(sysUser));
	}

	/**
	 * 店铺用户新增服务间调用
	 * @param sysUser
	 * @return R
	 */
	@Inside
	@ApiOperation(value = "店铺用户新增服务间调用")
	@PostMapping("/inside")
	public R saveInside(@RequestBody SysUser sysUser) {
		sysUser.setType(CommonConstants.USER_TYPE_2);
		sysUser.setOrganId(TenantContextHolder.getTenantId());
		//获取店员角色信息
		SysRole sysRole = sysRoleService.getOne(Wrappers.<SysRole>update().lambda()
				.eq(SysRole::getRoleCode,CommonConstants.ROLE_CODE_SHOP_USER));
		if(sysRole == null){
			return R.failed("请先添加角色编码为【"+CommonConstants.ROLE_CODE_SHOP_USER+"】的店员角色");
		}
		return R.ok(shopUserService.save(sysUser,sysRole.getId()));
	}
}
